package org.example.brave.http;

import brave.ErrorParser;
import brave.SpanCustomizer;
import brave.propagation.CurrentTraceContext;

/** Added to allow us to keep compatibility with deprecated {@link HttpTracing#serverParser()} */
@Deprecated final class HttpServerParserAdapter extends HttpServerParser {
    final HttpRequestParser requestParser;
    final HttpResponseParser responseParser;
    final CurrentTraceContext currentTraceContext;
    final ErrorParser errorParser;

    HttpServerParserAdapter(
            HttpRequestParser requestParser,
            HttpResponseParser responseParser,
            CurrentTraceContext currentTraceContext,
            ErrorParser errorParser
    ) {
        this.requestParser = requestParser;
        this.responseParser = responseParser;
        this.currentTraceContext = currentTraceContext;
        this.errorParser = errorParser;
    }

    @Override protected ErrorParser errorParser() {
        return errorParser;
    }

    @Override
    public <Req> void request(HttpAdapter<Req, ?> adapter, Req req, SpanCustomizer customizer) {
        HttpRequest request;
        if (req instanceof HttpServerRequest) {
            request = new HttpServerAdapters.FromRequestAdapter<>((HttpServerAdapter) adapter, req);
        } else if (adapter instanceof HttpServerAdapters.ToRequestAdapter) {
            request = ((HttpServerAdapters.ToRequestAdapter) adapter).delegate;
        } else {
            throw new AssertionError("programming bug");
        }
        requestParser.parse(request, currentTraceContext.get(), customizer);
    }

    @Override public <Resp> void response(HttpAdapter<?, Resp> adapter, Resp res, Throwable error,
                                          SpanCustomizer customizer) {
        HttpResponse response;
        if (res instanceof HttpServerResponse) {
            response =
                    new HttpServerAdapters.FromResponseAdapter<>((HttpServerAdapter) adapter, res, error);
        } else if (adapter instanceof HttpServerAdapters.ToResponseAdapter) {
            response = ((HttpServerAdapters.ToResponseAdapter) adapter).delegate;
        } else {
            throw new AssertionError("programming bug");
        }
        responseParser.parse(response, currentTraceContext.get(), customizer);
    }
}